

/**
 * 邮件菜单
 * @return
 */
function mailMenu() {
	var menu = $("#treeboxbox_tree") ;
	menu.html("加载中...") ;
	$.post("/share/supersystem/mail/mailMenu.jsp?d="+new Date().getTime(),null,function(data){
		menu.html(data);
	});
}

function readMailListModel(ref) {
	var url = "/share/mailJson!readMailList.action"+(ref!=undefined?"?d="+new Date().getTime():"") ;
	$.get(url,null,function(data){
		var dt = eval("("+data+")");
		if(dt.result == true) {
			window.open("/share/supersystem/mail/inBoxModel.jsp","right") ;
		}
	});
}


/**
 * 复选框操作，删除，编辑
 * @return
 */
function delMail(gourl) {
	var allchk = $("input[@name=mid][@checked]");
	
	var str ="" ;
	$.each(allchk,function(){
		str +=$(this).val()+"," ;
	});
	if(str != "") {
		str = str.substring(0, str.length - 1) ;
		$.post(gourl+encodeURIComponent(str) + "&d="+new Date().getTime(),null,function(data){
			var retData = eval("("+data+")") ;
			
			if(retData.result == true ) {
				toolTips("已删除【"+allchk.length+"】封邮件") ;
				readMailList();
			} else {
				toolTips("删除邮件失败...") ;
			}
 		});
	} else {
		toolTips("请先选择一条记录!");
	}
	
}

/**
 * 获得所有邮件列表
 * @return
 */
function readMailList() {
	var t = $("#mailList") ;
	var count = $("#count") ;
	var unreadcount = $("#unreadcount") ;
	var readcount = $("#readcount") ;
	t.html("<tr class='dataTableBody'><td align='center' colspan='5' height='150'>正在加载中...<img src='/share/images/wait.gif'></td></tr>") ;
	
	$.post("/share/mailJson!readMailListJSON.action?d="+new Date().getTime(),null,function(data){
		t.empty() ;
		var retData = eval("("+data+")") ;
		
		count.html(retData.inbox.messageCount) ;	//邮件总数量
		unreadcount.html((retData.inbox.unReaderMessageCount==0?"0":retData.inbox.unReaderMessageCount)) ;	//未读邮件数量
		readcount.html((retData.inbox.messageCount-retData.inbox.unReaderMessageCount==0?"0":retData.inbox.messageCount-retData.inbox.unReaderMessageCount)) ;	//未读邮件数量
		
		if(retData.inbox.mailBasicInfo.length > 0) {
			$.each(retData.inbox.mailBasicInfo,function(i,n){
			
				var _tr = $("<tr>") ;
				_tr.attr("class","dataTableBody") ;
				
				//邮件的ID号，设置到td中，隐藏该td
				var _tdid = $("<td>") ;_tdid.html(n.messageId) ;_tdid.hide() ;
				
				var _td1 = $("<td>") ;var _td2 = $("<td>") ;var _td3 = $("<td>") ;var _td4 = $("<td>") ;
				
				//获得相关信息判断是否读邮件
				var rMsgUID = retData.inbox.readMsgUID ;
				rMsgUID = rMsgUID.substring(0, rMsgUID.length-1) ;
				var strs= new Array();
				strs = rMsgUID.split(",") ;
				
				for(var j=0;j<strs.length; j++) {
					if(strs[j] == n.msgUID) {
						_td1.html("<input type='checkbox' name='mid' value='"+n.msgUID+"'>") ;
						_td2.html("<span class='unreaderfont'>"+n.messageFrom+n.msgUID+"</span>") ;
						_td3.html("<span class='unreaderfont'>"+n.messageSubject+"====["+n.messageId+"]</span>") ;
						_td4.html("<span class='unreaderfont'>"+n.messageDate+"</span>") ;
						break ;
					} else { 
						_td1.html("<input type='checkbox' name='mid' value='"+n.msgUID+"'>") ;
						_td2.html("<b class='unreaderfont'>"+n.messageFrom+n.msgUID+"</b>") ;
						_td3.html("<b class='unreaderfont'>"+n.messageSubject+"====["+i+"]</b>") ;
						_td4.html(n.messageDate) ;
					}
				}
				
				_tr.append(_tdid) ;
				_tr.append(_td1) ;
				_tr.append(_td2) ;
				_tr.append(_td3) ;
				_tr.append(_td4) ;
				
				_tr.children("td:gt(1)").css("cursor","pointer");
				_tr.children("td:gt(1)").click(function() {
					openMail(_tr.children().eq(0).text()) ;
				});
				
				t.append(_tr) ;
			});
		} else {
			var _tr = $("<tr>") ;
			_tr.attr("class","dataTableBody") ;
			
			var _tdid = $("<td colspan='5'>") ;
			_tdid.attr("height","100") ;
			_tdid.html("收件箱&nbsp;没有任何邮件") ;
			_tdid.css("font-size","12px").css("font-weight","bold") ;
			
			_tr.append(_tdid) ;
			t.append(_tr) ;
		}
	});
}

/**
 * 打开选择邮件
 * @return
 */
function openMail(mid) { 
	window.location.href= "mail!openMail.action?baseform.param="+mid ; 
}

/**
 * 发送邮件
 * @return
 */
function sendMail() {
	
	var from = $("#fromAddr").text();
	var recipientTO = $("#recipientTO").val();
	var subject = $("#subject").val();
	var content = $("#content").val();
	var priority = $("#priority") ;
	var textType = $("#text") ;
	var cc = $("#recipientCC").find("input").val() ;
	var bcc = $("#recipientBCC").find("input").val() ;

	var mailinfo = {"miv.fromAddr":from,"miv.recipientTO":recipientTO,"miv.subject":subject,"miv.content":content,"miv.priority":(priority.attr("checked") == true ? priority.val():''),"miv.contentType":(textType.attr("checked") == true ? textType.val():''),"miv.recipientCC":(cc!=""?cc:""),"miv.recipientBCC":(bcc!=""?bcc:"")} ;
	
	$.post("/share/mailJson!sendMailAction.action?d="+new Date().getTime(),mailinfo,function(data){
		var retData = eval("("+data+")") ;
		if(retData.result == true) {
			alert("发送成功") ;
		} else {
			alert("发送失败") ;		
		}
	});
	
	//alert("from:" + from +"\r\n" + "recipientTO:" + recipientTO + "\r\n" + "subject:" + subject + "\r\n" + "content:" + content + "\r\n" + "priority:" + priority) ;
}


/** 显示和隐藏 抄送、密送 */
function toCC() {
	var s = $("#cc") ;
	var cc = $("#recipientCC") ;

	s.attr("href","javascript:ccTO();");

	cc.show() ;
}
function ccTO() {
	var s = $("#cc") ;
	var cc = $("#recipientCC") ;

	s.attr("href","javascript:toCC();");
	
	cc.find("input").val("") ;
	cc.hide() ;
}
function toBCC() {
	var s = $("#bcc") ;
	var bcc = $("#recipientBCC") ;

	s.attr("href","javascript:bccTO();");

	bcc.show() ;
}
function bccTO() {
	var s = $("#bcc") ;
	var bcc = $("#recipientBCC") ;

	s.attr("href","javascript:toBCC();");
	bcc.find("input").val("") ;
	bcc.hide() ;
}










